package com.hq.fxh.CustomerView.countview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hq.fxh.R;
import com.hq.fxh.Utils.DensityUtils;

import java.lang.reflect.Method;


/**
 * Created by 大圣  on 2015/7/29 0029.
 */
public class EmptyView extends RelativeLayout {

    private String mText;
    private String mLoadingText;

    private TextView mTextView;
    private ImageView imageView;
    private Button mButton;
    private View mBindView;

    public EmptyView(Context context){
        this(context,null);
    }
    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EmptyView, 0, 0);
        String text = ta.getString(R.styleable.EmptyView_android_text);
        String buttonText = ta.getString(R.styleable.EmptyView_buttonText);
        mLoadingText = ta.getString(R.styleable.EmptyView_loadingText);
        ta.recycle();
        init(text, buttonText);
    }


    private void init(String text, String buttonText) {
        if(TextUtils.isEmpty(text)) text = "暂无数据";
        if(TextUtils.isEmpty(buttonText)) buttonText = "刷新";
        if(TextUtils.isEmpty(mLoadingText)) mLoadingText = "加载中...";
        mText = text;

        mTextView = new TextView(getContext());
        mTextView.setText(text);

        RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(
                DensityUtils.dip2px(getContext(),200), LayoutParams.WRAP_CONTENT);
        textViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        mTextView.setId(R.id.id_empty_text);
        mTextView.setGravity(Gravity.CENTER);
        addView(mTextView, textViewParams);
//        imageView = new ImageView(getContext());
//        RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
//                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
//        imageParams.addRule(RelativeLayout.RIGHT_OF, R.id.id_empty_text);
//        imageView.setImageResource(R.drawable.icon_image_load_logo);
//        addView(imageView, imageParams);
        mButton = new Button(getContext());
        mButton.setText(buttonText);
        mButton.setTextColor(getResources().getColor(R.color.greydivide));
        mButton.setBackgroundDrawable(null);
        RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        buttonParams.addRule(RelativeLayout.BELOW, R.id.id_empty_text);
        setVisibility(View.INVISIBLE);
        addView(mButton, buttonParams);
    }

    public void bindView(View view) {
        mBindView = view;
    }

    public void loading() {
      loading(mLoadingText);
    }
    public void loading(String message){
        if(mBindView != null) mBindView.setVisibility(View.GONE);
        setVisibility(View.VISIBLE);
        mButton.setVisibility(View.INVISIBLE);
        mTextView.setText(message);

        mTextView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.icon_re),null,null,null);
    }

    public void success() {
        setVisibility(View.GONE);
        if(mBindView != null) mBindView.setVisibility(View.VISIBLE);
    }

    public void empty() {
       empty(mText);
    }
    public void empty(String message){
        if(mBindView != null) mBindView.setVisibility(View.GONE);
        setVisibility(View.VISIBLE);
        mButton.setVisibility(View.VISIBLE);
        mTextView.setText(message);

    }

    public void buttonClick(final Object base, final String method,
                            final Object... parameters) {
        mButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int length = parameters.length;
                Class<?>[] paramsTypes = new Class<?>[length];
                for (int i = 0; i < length; i++) {
                    paramsTypes[i] = parameters[i].getClass();
                }
                Method m = null;
                for(Class<?> clazz = base.getClass(); clazz != Object.class ; clazz = clazz.getSuperclass()) {
                    try {
                        m = clazz.getDeclaredMethod(method, paramsTypes) ;
                        m.setAccessible(true);
                        m.invoke(base, parameters);
                    } catch (Exception e) {
                    }
                }
//                try {
//                    Method m = base.getClass().getDeclaredMethod(method, paramsTypes);
//                    m.setAccessible(true);
//                    m.invoke(base, parameters);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }
        });
    }
}

